package com.ztom.top100;

/**
 * 汉明距离
 * <p>
 * https://leetcode-cn.com/problems/hamming-distance/
 *
 * @author ZhangTao
 */
public class Code91HammingDistance {

    public int hammingDistance1(int x, int y) {
        return Integer.bitCount(x ^ y);
    }

    public int hammingDistance(int x, int y) {
        int res = 0;
        int a = x ^ y;
        while (a != 0) {
            // 消除最右侧的 1
            a &= (a - 1);
            res++;
        }
        return res;
    }
}
